package keeto.com.vn.classified.utils;

import android.content.Context;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

public class UtilityListView {
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
			//listItem.measure(0, 0);
			totalHeight += (listItem.getMeasuredHeight() -30);
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
		listView.setLayoutParams(params);
		listView.requestLayout();
	}

	public static void setListViewHeightBasedOnChildrenforList(Context ctx,ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
		//        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			 if (listItem instanceof ViewGroup) {
                 listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
              }
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		int height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
		//        params.height  = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, ctx.getResources().getDisplayMetrics());
		params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
		listView.setLayoutParams(params);
		listView.requestLayout();
	}

	public static void setListViewHeightBasedOnChildrenforListTest(Context ctx,ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		int totalHeight = 0;
		//        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i,null , listView);
			 if (listItem instanceof ViewGroup) {
                 listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
              }
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		int height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
		//        params.height  = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, ctx.getResources().getDisplayMetrics());
		params.height = ((int)(1.1*totalHeight) + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
//		Log.i("DATA", " HEI : " + totalHeight + "             " +(3*totalHeight)/2+ "       " + 2*totalHeight + " jhsd " +params.height);

		listView.setLayoutParams(params);
		listView.requestLayout();
	}
	public static void setListViewHeightforOrder(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			//            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
			listItem.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
			totalHeight += listItem.getMeasuredHeight();

		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
		listView.setLayoutParams(params);
		listView.requestLayout();
	}

	public static void setListViewHeightBasedOnChildrenforWarranty(Context ctx,ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		int count = listAdapter.getCount();
		//        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
		for (int i = 0; i < count; i++) {
			View listItem = listAdapter.getView(i, null, listView);
			//            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams() ;
		int height = (totalHeight + (listView.getDividerHeight() * (count - 1))) + count*9;
		//        int height = (totalHeight + (listView.getDividerHeight() * (count - 1)));
		//        params.height  = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, ctx.getResources().getDisplayMetrics());
		params.height = height ;
		listView.setLayoutParams(params);
		listView.requestLayout();
	}
}